import axios from 'axios';
import { getVersion } from '../utils/request';
import { config, request } from '../configs';
import { paramsControl } from '../utils/util';
import makeApi from './index';

const { appKey } = config;
export const api = {
	query: '/device/device/list',
	detail: '/device/device/detail',
	update: '/device/device/update',
	control: '/device/device/fire',
	operatorList: '/product/productOperationHistory/list',
	create: '/device/device/add',
	runningRecordList: '/device/deviceRunningRecord/list',
	getDeviceMapList: '/device/map/list',
	getDeviceMapDetail: '/device/map/detail',
	deleteDevice: '/device/device/delete',
	getDeviceByUser: '/benefit/shareBenefitRule/devices',
	assignDevice: '/device/device/assign',
	getPreAssign: '/device/device/preAssign',
	unbind: '/device/device/unbind',
	exportQrcode: '/device/qrcode/export',
	importDevice: '/device/qrcode/upload',
	realStatus: '/device/weather/detail',
	deviceReportShow: '/device/device/show',
	unLockDevice: '/device/device/unlock',
	deviceEditMode: '/device/device/addMoreMode',
	assignDeviceBatch: '/device/device/assignModeOrArea',
	getDeviceBindUserList: '/device/userBindDevice/user/list',
	listDeviceNotInContract: '/device/device/listDeviceNotInContract',
	listDeviceByContractId: '/device/device/listDeviceByContractId', // 合同已关联的设备列表
	userBindDeviceList: '/device/userBindDevice/device/list', // 用户已绑定的设备列表
	dataDetail: '/device/device/alarmDetailList'
};

export const defaultData = {
	query: {
		size: 10,
		current: 1,
		asc: false,
		orderByField: 'utime'
	}
};
const services = makeApi(api);

// 导出二维码
services.exportQrcode = function exportQrcode(params, onDownloadProgress) {
	return axios.post(api.exportQrcode, {
		appKey,
		data: params,
		version: getVersion(api.exportQrcode)
	}, {
			responseType: 'blob',
			onDownloadProgress,
			headers: {
				Version: getVersion(api.exportQrcode)
			}
		});
};

// 导入设备
services.importDevice = async function importDevice(file, productId, onDownloadProgress) {
	const data = new FormData();
	data.append('file', file, file.name);
	if (productId) {
		data.append('productId', productId);
	}
  const res = await axios.post(api.importDevice, data, {
		onDownloadProgress,
		headers: {
			Authorization: window.localStorage.getItem('access_token'),
			Version: getVersion(api.exportQrcode)
		}
	});
	return res.data;
};

services.dataDetail = paramsControl((mac) => {
		return request({
			url: `${api.dataDetail}?mac=${mac}`,
			method: 'post',
		});
});

const {
	query,
	detail,
	update,
	control,
	operatorList,
	create,
	realStatus,
	runningRecordList,
	getDeviceMapList,
	getDeviceMapDetail,
	deleteDevice,
	getDeviceByUser,
	assignDevice,
	getPreAssign,
	unbind,
	exportQrcode,
	importDevice,
	deviceReportShow,
	unLockDevice,
	deviceEditMode,
	assignDeviceBatch,
	getDeviceBindUserList,
	listDeviceNotInContract,
	listDeviceByContractNo,
	userBindDeviceList,
	listDeviceByContractId,
	dataDetail
} = services;

export {
	query,
	detail,
	update,
	control,
	realStatus,
	operatorList,
	create,
	runningRecordList,
	getDeviceMapList,
	getDeviceMapDetail,
	deleteDevice,
	getDeviceByUser,
	assignDevice,
	getPreAssign,
	unbind,
	exportQrcode,
	importDevice,
	deviceReportShow,
	unLockDevice,
	deviceEditMode,
	assignDeviceBatch,
	getDeviceBindUserList,
	listDeviceNotInContract,
	listDeviceByContractNo,
	userBindDeviceList,
	listDeviceByContractId,
	dataDetail
};

export default services;
